﻿using System;
using System.Collections.Generic;
using XCI.Core;
using XCI.Windows.Extensions;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统对象数据关联服务
    /// </summary>
    public class DataMapService : BaseService<object>
    {
        public static readonly DataMapService Instance = new DataMapService();
        protected override string RootUrl => "/api/sys/dataMap/";

        /// <summary>
        /// 保存对象数据关联
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <param name="objectName">对象名称</param>
        /// <param name="targetName">目标名称</param>
        /// <param name="targetIds">目标主键字符串</param>
        public bool SaveTargets(string objectId, string objectName, string targetName, string targetIds)
        {
            var url = GetUrl("saveTargets");
            var map = Map.New
                .Add("objectId", objectId)
                .Add("objectName", objectName)
                .Add("targetName", targetName)
                .Add("targetIds", targetIds);
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 根据对象删除数据关联
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <param name="objectName">对象名称</param>
        /// <param name="targetName">目标名称</param>
        public bool DeleteByObject(string objectId, string objectName, string targetName)
        {
            var url = GetUrl("deleteByObject");
            var map = Map.New
                .Add("objectId", objectId)
                .Add("objectName", objectName)
                .Add("targetName", targetName);
            return SysRuntime.Api.PostAsMessage(url, map).EnsureSuccess().Success;
        }

        /// <summary>
        /// 获取目标数据关联主键列表
        /// </summary>
        /// <param name="objectId">对象主键</param>
        /// <param name="objectName">对象名称</param>
        /// <param name="targetName">目标名称</param>
        public List<string> selectTargets(string objectId, string objectName, string targetName)
        {
            var url = GetUrl("selectTargets");
            var map = Map.New
                .Add("objectId", objectId)
                .Add("objectName", objectName)
                .Add("targetName", targetName);
            return SysRuntime.Api.Post<List<string>>(url, map).EnsureSuccess().Data;
        }
    }
}
